package com.situ.mall.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.mall.common.entity.Category;
import com.situ.mall.common.exception.BusinessException;
import com.situ.mall.common.mapper.CategoryMapper;
import com.situ.mall.common.service.ICategoryService;
import com.situ.mall.common.vo.IndexCategoryVO;
import com.situ.mall.common.vo.TableVO;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kkkkkk
 * @since 2023-09-11
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public TableVO tree() {
        TableVO vo = new TableVO();
        vo.setData(baseMapper.tree());
        return vo;
    }

    @CacheEvict(value = "categoryCache")
    @Override
    public void doDelete(Integer id) {
        List<Category> list = baseMapper.selectList(new LambdaQueryWrapper<Category>().eq(Category::getParentId, id));
        if (!list.isEmpty()) {
            throw new BusinessException("存在子分类不能删除！");
        }
        //TODO 检查商品是否选择过
        baseMapper.deleteById(id);
    }

    @Cacheable(value = "categoryCache")
    @Override
    public List<IndexCategoryVO> indexList() {
        return baseMapper.indexList();
    }
}
